<%@ page language="java" pageEncoding="utf-8" contentType="text/html; charset=utf-8"%>
<%@ taglib prefix="ww" uri="webwork" %>
<ww:bean name="'com.css.apps.base.dict.service.DictMan'" id="dictID" />
<ww:bean name="'com.css.apps.base.sysjob.service.SysJobService'" id="sysPostService" />
<style>
	.select_auth_Role{
	    margin-top: 5px;
    	overflow-y: auto;
    	height: 270px;
   	 	width: 310px;
    }
    .select_role{
    	overflow-y: auto;
    	height: 300px;
    }
</style>
<form class="form-horizontal form-validate" id="getorgpostsysroles_from" method="post" <ww:if test="uuid != null">action="updOrgPostSysRoles.action"</ww:if><ww:else>action="addOrgJobSysRoles.action"</ww:else> onsubmit="return $action.submit(this);">
	<input type="hidden" name="uuid" id="uuid" value="<ww:property value='item.uuid'/>" >
	<input type="hidden" name="orgId" id="orgId" value="<ww:property value='orgId'/>" >
	<input type="hidden" name="jobId" id="jobId" value="<ww:property value='jobId'/>" >
	<input type="hidden" name="applyPermission" id="applyPermission" value="<ww:property value='applyPermission'/>" >
	<div class="content" style="min-width: 500px;">
		<table class="table  table-nobordered editTable form-group-sm">
			<tr class="firstRow">
				<td colspan="1" class="col-md-1"></td>
				<td colspan="1" class="col-md-1"></td>
				<td colspan="1" class="col-md-1"></td>
				<td colspan="1" class="col-md-1"></td>
				<td colspan="1" class="col-md-1"></td>
				<td colspan="1" class="col-md-1"></td>
				<td colspan="1" class="col-md-1"></td>
				<td colspan="1" class="col-md-1"></td>
				<td colspan="1" class="col-md-1"></td>
				<td colspan="1" class="col-md-1"></td>
				<td colspan="1" class="col-md-1"></td>
				<td colspan="1" class="col-md-1"></td>
			</tr>
			<tr>
				<td class="blackGridBR text-center" align="center" colspan="6" style="color: blue;align-content: center;">
					<ww:property value="orgName"/>
				</td>
				<td class="blackGridBR text-center" align="center"  colspan="6" style="color: blue;align-content: center;">
					<ww:property value="jobName"/>
				</td>
			</tr>
			<tr>
				<!-- <td class="blackGridBR" align="center"  colspan="2" style="color: blue;">
					*开通权限
				</td> -->
				<td class="blackGridBR" align="center" colspan="6" id="selectRoletd1">
					<div>
						<select id="sysIdsearch" name="sysIdsearch" style="width: 300px;border-bottom: 1px solid #DDDDDD;" onchange="search(this)">
							<option value="">---------------请选择系统---------------</option>
								<ww:iterator value="#sysPostService.getOpenSystems()">
									<option value="<ww:property value='uuid'/>"><ww:property value="name"/></option>
								</ww:iterator>
						</select>
					</div>
					<div class="select_auth_Role">
						<table class="table table-bordered">
							<thead>
								<tr>
									<th colspan="3">选择权限</th>
								</tr>
							</thead>
							<tbody id="sys_Role_div">
								
							</tbody>
						</table>
					</div>
				</td>
				<td class="blackGridBR" colspan="6" id="selectRoletd2" width="460px">
					<div class="select_role">
						<table class="table table-bordered">
							<thead>
								<tr>
									<th width="80px">系统</th>
									<th width="150px">开通权限</th>
								</tr>
							</thead>
							<tbody id="selectRole">
								<ww:iterator id="sysData" value="#selectSys">
									<tr>
										<td>
											<ww:property value="#sysPostService.getSystemName(key)"/>
										</td>
										<input type="hidden" id="sysid_<ww:property value='key'/>" name="" value="<ww:property value='key'/>"> 
										<td>
											<ul id="ul_<ww:property value='key'/>">
												<ww:iterator id="itemList" value="value">
													<li><ww:property value='roleName'/></li>
													<input type="hidden" id="roleid_<ww:property value='roleId'/>" name="" value="<ww:property value='roleId'/>">
												</ww:iterator>
											</ul>
										</td>
									</tr>
								</ww:iterator>
							</tbody>
						</table>
					</div>	
				</td>
			</tr>
		</table>
	</div>

	<div class="set-btn" data-spy="affix" data-offset-top="200">
		<!-- <button class="btn btn-sm btn-primary" type="button"
			onclick="$(this).submit();">
			<i class="fa fa-save"></i> 保存
		</button> -->
		<button class="btn btn-sm btn-primary" type="button"
			onclick="sysRolesSubmitBefore();">
			<i class="fa fa-save"></i> 保存
		</button>
		<a class="btn btn-sm btn-default" href="javascript:;"
			onclick="$css.closeDialog()"> <i class="fa fa-close"></i> 关闭
		</a>
	</div>
</form>
<script>
function search(parm){
	var $tab = $.cssTab.focus();
	var $form = $("#getorgpostsysroles_from",$tab);
	var sysId = $("#"+parm.id).val();
	if(isnull(sysId)){
		return false;
	}
	var url = "dirSysRoles.action"
	var parm = {
		"sysId": sysId
	}
	$.post(url, parm , function(data){
		var htm = "";
		if(data.result == "0"){
			if(isnull(data.info)){
				return false;
			}
			var len = data.info.length;
			$("tbody #sys_Role_div").html(htm);
			var selectArray = $("#ul_"+sysId+" input[type=hidden]");
			for(var i= 0 ; i< len; i +=3){
				htm += '<tr>'+
					'  <td class="text-center" width="33%">'+
					' <label><input type="checkbox" id='+data.info[i].uuid+' name='+data.info[i].name+'  value='+data.info[i].uuid+' ' ;
					/* 添加复选  */
					$.each(selectArray, function(j, obj){
						if(data.info[i].uuid == obj.value){
							htm += ' checked=checked ';
						}
					});
					htm += ' />'+data.info[i].name+' </label>'+
					' </td>';
					if(i+1 < len){
						htm += ' <td class="text-center" width="33%">'+
						' <label><input type="checkbox" id='+data.info[i+1].name+' name='+data.info[i+1].name+'  value='+data.info[i+1].uuid+' ';
						$.each(selectArray, function(j, obj){
							if(data.info[i+1].uuid == obj.value){
								htm += ' checked=checked ';
							}
						});
						htm += ' />'+data.info[i+1].name+' </label> '+
						' </td>';
					}else if(len > 2){
						htm += ' <td class="text-center" width="33%"></td>';
					}
					if(i+2 < len){
						htm += ' <td class="text-center" width="33%">'+
						' <label><input type="checkbox" id='+data.info[i+2].name+' name='+data.info[i+2].name+' value='+data.info[i+2].uuid+' ';
						$.each(selectArray, function(j, obj){
							if(data.info[i+2].uuid == obj.value){
								htm += ' checked=checked ';
							}
						});
						htm += ' />'+data.info[i+2].name+' </label>'+
						' </td>';
					}else if(len > 3){
						htm += ' <td class="text-center" width="33%"></td>';
					}
				htm += ' </tr>';
			}
			$("tbody #sys_Role_div").html(htm);
			htm = "";
		}else {
			$("tbody #sys_Role_div").html(htm);
			$css.tip(data.msg);
		}
	},'json');
}
$(document).unbind('change').on("change", "#sys_Role_div input[type='checkbox']",function(event){
	event.stopPropagation();
	var flag = $(this).is(':checked');
	if (flag) {
		addHtml(this);
	}else {
		removeRole(this);
	}
	return false;
})
function addHtml(thisObj){
	var $tab = $.cssTab.focus();
	var $form = $("#getorgpostsysroles_from");
	var sysName = $("#sysIdsearch", $form).find("option:selected").text();
	var sysId = $("#sysIdsearch", $form).val();
	var idLength = $("#sysid_"+sysId).length;
	var htm = "<tr>";
	if (idLength == 0) {
		htm += '<td>'+sysName+'</td>'+
		'<input type="hidden" id="sysid_'+sysId+'" name="" value='+sysId+' />'+
		'<td><ul id="ul_'+sysId+'" ><li>'+thisObj.name+'</li>'+
		'<input type="hidden" id="roleid_'+thisObj.value+'" name="" value='+thisObj.value+' />'+
		'</ul></td>'+
		'</tr>';
		$("#selectRole", $form).append(htm);
		htm = "";
	}else {
		var htmlRole = '<li>'+thisObj.name+'</li>'+
		'<input type="hidden" id="roleid_'+thisObj.value+'" name="" value='+thisObj.value+' />'; 
 		$('#ul_'+sysId, $form).append(htmlRole);
 		htmlRole="";
	}
}
function removeRole(thisObj){
	var $tab = $.cssTab.focus();
	var $form = $("#getorgpostsysroles_from");
	$("#roleid_"+thisObj.value, $form).prev('li').remove();
	$("#roleid_"+thisObj.value, $form).remove();
	var sysid= $("#sysIdsearch", $form).val();
	var lilength = $("#ul_"+sysid).find('li').length;
	if(lilength == 0){
		$("#sysid_"+sysid).parent('tr').remove();
	}
}
function sysRolesSubmitBefore(){
	var $tab = $.cssTab.focus();
	var $form = $("#getorgpostsysroles_from");
	var applyPermission ={};
	var selectSysIds = $("#selectRole>tr>input[type=hidden]", $form);
	var roleArray = [];
	var pams = "";
	
	$.each(selectSysIds, function(i , obj){
		var seleParms = "";
		if(isnull(seleParms)){
			seleParms = obj.value +':';
		}
		var selectRoleIds = $("#ul_"+obj.value + ">input[type=hidden]", $form);
		$.each(selectRoleIds , function(j, obj2){
			if(j == selectRoleIds.length-1){
				seleParms += obj2.value ;
			}else{
				seleParms += obj2.value + ',';
			}
		});
		if(isnull(pams)){
			pams += seleParms;
		}else{
			pams += "@"+seleParms;
		}
		roleArray.length = 0;
	});
	$("#applyPermission", $form).val(pams);
	$form.submit();
}
</script>